package com.bsoft.exchange.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsoft.exchange.dao.his.RosteringMapper;
import com.bsoft.exchange.domain.ListRosteringDeptDomain;
import com.bsoft.exchange.domain.ListRosteringDomain;
import com.bsoft.exchange.pojo.ListRosteringPojo;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.DateUtil;

@Service
public class RosteringService {
	
	@Autowired
	RosteringMapper rosteringMapper;

	
	/**
	 * 获取我的排班。
	 * @return
	 */
	public BaseResponse<List<ListRosteringDomain>> listRostering(ListRosteringPojo listRosteringPojo){
		
		BaseResponse<List<ListRosteringDomain>> response = new BaseResponse<>();
		response.setCode(Constant.successResponse);
		response.setData(null);
		try {
			
			 Long jgid;
			 String ysdm;
	         if(listRosteringPojo.getOrganizationId() == null || "".equals(listRosteringPojo.getOrganizationId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医院识别代码不能为空!");
	        	  
	             return response;
	         }
	           
	        jgid = Long.parseLong(listRosteringPojo.getOrganizationId());
			
	        if(listRosteringPojo.getDoctorId() == null || "".equals(listRosteringPojo.getDoctorId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医生id不能为空!");
	        	  
	             return response;
	         }
	           
	        ysdm = listRosteringPojo.getDoctorId();
	        
	        Integer weekRange = 0;
	        weekRange = listRosteringPojo.getWeekRange();
	        if(weekRange == null){
	        	weekRange =0;
	        }
	       //获取当前周几
	        Date today = new Date();
	        Integer week = DateUtil.getDayofweek(today);
	        
	        //计算出需要的开始日期，本周日（1-week） + 7*周次（负数往前减）
	        Date beginDate = DateUtil.getDateByZeroTime(today, 1 - week + 7*weekRange); 
	        Date endDate = DateUtil.getDateByZeroTime(beginDate, 7); //往后加7天 下个星期日   计算时 < 该日期
	        
	        List<ListRosteringDomain> rosList = rosteringMapper.listRostering(jgid, ysdm,beginDate,endDate);
	        if(rosList == null ||rosList.size() < 1){
	        	response.setMessage("未查询到数据！");
	        }else{
	        	response.setMessage("处理成功");
				response.setData(rosList);
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(Constant.errorTag);
			response.setMessage("处理失败"+e.getMessage());
		}
		
		
		return response;
	}
	
	
	/**
	 * 获取科室排班。
	 * @return
	 */
	public BaseResponse<List<ListRosteringDeptDomain>> listRosteringDept(ListRosteringPojo listRosteringPojo){
		
		BaseResponse<List<ListRosteringDeptDomain>> response = new BaseResponse<>();
		response.setCode(Constant.successResponse);
		response.setData(null);
		try {
			
			 Long jgid;
			 String ysdm;
	         if(listRosteringPojo.getOrganizationId() == null || "".equals(listRosteringPojo.getOrganizationId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医院识别代码不能为空!");
	        	  
	             return response;
	         }
	           
	        jgid = Long.parseLong(listRosteringPojo.getOrganizationId());
			
	        if(listRosteringPojo.getDoctorId() == null || "".equals(listRosteringPojo.getDoctorId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医生id不能为空!");
	        	  
	             return response;
	         }
	           
	        ysdm = listRosteringPojo.getDoctorId();
	        
	        Integer weekRange = 0;
	        weekRange = listRosteringPojo.getWeekRange();
	        if(weekRange == null){
	        	weekRange =0;
	        }
	       //获取当前周几
	        Date today = new Date();
	        Integer week = DateUtil.getDayofweek(today);
	        
	        //计算出需要的开始日期，本周日（1-week） + 7*周次（负数往前减）
	        Date beginDate = DateUtil.getDateByZeroTime(today, 1 - week + 7*weekRange); 
	        Date endDate = DateUtil.getDateByZeroTime(beginDate, 7); //往后加7天 下个星期日   计算时 < 该日期
	        
	        List<ListRosteringDeptDomain> rosList = rosteringMapper.listRosteringDept(jgid, ysdm,beginDate,endDate);
	        if(rosList == null ||rosList.size() < 1){
	        	response.setMessage("未查询到数据！");
	        }else{
	        	response.setMessage("处理成功");
				response.setData(rosList);
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(Constant.errorTag);
			response.setMessage("处理失败"+e.getMessage());
		}
		
		
		return response;
	}
	
}
